﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL.ComplexClasses;
using DAL;
using App_Log;
using App_Utils;


namespace BLL
{
    public class BL_Address
    {
        public static Address SaveRecord(Address objAddress)
        {
            try
            {
                clsLog.logMsg("Address", "SaveRecord", "Started");
                DAL.ithubEntities db = new ithubEntities();
                var query = (from q in db.tblAddresses where q.address_id == objAddress.address_id select q).SingleOrDefault();

                if (query != null)
                {
                    query = (tblAddress)clsUtils.ConvertToObject(objAddress, new tblAddress());
                    db.SaveChanges();
                }
                else
                {
                    tblAddress _objtblAddress = new tblAddress();
                    _objtblAddress = (tblAddress)clsUtils.ConvertToObject(objAddress, new tblAddress());
                    db.tblAddresses.Add(_objtblAddress);
                    db.SaveChanges();
                    objAddress = (Address)clsUtils.ConvertToObject(_objtblAddress, objAddress);
                }
            }
            catch (Exception ex)
            {
                clsLog.logError("Address", "SaveRecord", ex.Message);
            }
            return objAddress;
        }

        public static List<Address> GetAddressInfo(long id, string module)
        {
            List<Address> objAddress = new List<Address>();
            try
            {
                clsLog.logMsg("Address", "GetAddressInfo", "Started");

                DAL.ithubEntities db = new ithubEntities();
                List<tblAddress> _objAddress = (from q in db.tblAddresses
                                          where q.id == id
                                              && q.module == module
                                              && q.is_visible == true
                                          select q).ToList();
                if (_objAddress.Count > 0)
                {
                    foreach (tblAddress item in _objAddress)
                    {
                        objAddress.Add((Address)App_Utils.clsUtils.ConvertToObject(item, new Address()));
                    }                    
                }
                else
                {
                    objAddress = null;
                }
            }
            catch (Exception ex)
            {
                clsLog.logError("Address", "GetAddressInfo", ex.Message);
            }
            return objAddress;
        }
    }
}
